/**
 * Created by Administrator on 2018/10/18.
 */

// 格式化状态
function statusFormatter(value) {
    var spanHtml = '';
    switch (value) {
        case '已提交':
            spanHtml = '<span class="label label-primary">已提交</span>';
            break;
        case '已完成':
            spanHtml = '<span class="label label-success">已完成</span>';
            break;
        case '已驳回':
            spanHtml = '<span class="label label-danger">已驳回</span>';
            break;
        case '进行中':
            spanHtml = '<span class="label label-info">进行中</span>';
            break;
        case '未开始':
            spanHtml = '<span class="label label-warning">未开始</span>';
            break;
        case '已超期':
            spanHtml = '<span class="label label-danger">已超期</span>';
            break;
    }
    // console.log(spanHtml);
    return spanHtml;
}

$(function () {
    var user = getUser();
    var roleIdListString = user.roleIdList.join(',');
    roleIdListString += ',0101';
    console.log(roleIdListString);
    $("#jqGrid").jqGrid({
        url: baseURL + 'generator/projectentity/list/' + user.userId + '/' + roleIdListString.split(','),
        datatype: "json",
        colModel: [
            {label: '项目编号', name: 'projectNumber', index: 'project_number', width: 80, sortable: true},
            {label: '项目名称', name: 'projectName', index: 'project_name', width: 80, sortable: false},
            {label: '项目状态', name: 'projectStatus', index: 'project_status', width: 80, sortable: false},
            {label: '当前任务', name: 'projectTask', index: 'projectTask', width: 80, sortable: false},
            {label: '相关线路', name: 'relevantLinesName', index: 'relevant_lines_name', width: 80, sortable: false},
            {
                label: '任务状态', name: 'projectTaskStatus', index: 'projectTaskStatus', width: 80, sortable: false,
                formatter: function (value) {
                    return statusFormatter(value);
                }
            },
            {
                label: '电压类型',
                name: 'voltageType',
                index: 'voltage_type',
                width: 80,
                sortable: false,
                formatter: function (value, options, row) {
                    console.log(value);
                    if (value == "1") {
                        return '<span >' + '中压' + '</span>';
                    } else {
                        return '<span >' + '高压' + '</span>';
                    }
                }
            },
            {label: '项目经理', name: 'projectManager', index: 'project_manager', width: 80, sortable: false},
            {label: '申报时间', name: 'projectTime', index: 'project_time', width: 80, sortable: true},
            {label: '开始时间', name: 'startTime', index: 'start_time', width: 80, sortable: false},
            {label: '结束时间', name: 'endTime', index: 'end_time', width: 80, sortable: false},
            {
                label: '操作', name: 'endTime', width: 80, sortable: false, formatter: function (value, options, row) {
                    console.log(row);
                    return '<a class="btn-details" href="javascript:void(0);" data-id="' + row.id + '">' + '详情' + '</a>';
                }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        multiSort: false,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            $('#jqGrid').on('click', '.btn-details', function (e) {
                var projectId = $(this).attr('data-id');
                var pWindow = window.parent;
                var oIframe = $('#main-content iframe', pWindow.document);
                // 改变首页的 indexVm 的 data，触发 updated
                pWindow.indexVm.main = 'modules/project/projectdetails.html?projectId=' + projectId;
                pWindow.location.hash = 'modules/project/projectdetails.html?projectId=' + projectId;
                oIframe.attr('src', 'modules/project/projectdetails.html?projectId=' + projectId);
            })
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        projectEntity: {},
        searchOptions: {
            projectStatus: '11',
            projectName: '',
            projectManager: '',
            projectNumber: '',
            voltageType: '',
            relevantLinesName:''
        },
        projectManagerList: []   //所有人员
    },
    mounted: function () {
        /* 选择项目经理 */
        var _this = this;
        $.get(baseURL + 'sys/user/queryAll', function (result) {
            _this.projectManagerList = result.userList;
            _this.projectManagerList.unshift({id: "0", text: "全部"});
            _this.searchOptions.projectManager = "0";
        });
        _this.searchOptions.voltageType = "0";
    },
    created: function () {
        // console.log(1);
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.projectEntity = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.projectEntity.id == null ? "generator/projectentity/save" : "generator/projectentity/update";
            vm.projectEntity.projectStatus = 0; // 创建时默认项目进行中
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.projectEntity),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        //删除
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "generator/projectentity/delete/" + "del",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        //终止
        ter: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            console.log(ids);
            for (var i = 0; i < ids.length; i++) {
                var rowData = $("#jqGrid").jqGrid('getRowData', ids[1]);
            }

            console.log("****终止****");
            console.log(rowData);
            confirm('确定要终止选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "generator/projectentity/delete/" + "ter",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        //恢复
        rec: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            console.log("****恢复****");
            console.log(ids);
            confirm('确定要恢复选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "generator/projectentity/delete/" + "rec",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "generator/projectentity/info/" + id, function (r) {
                vm.projectEntity = r.projectEntity;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            // console.log(page);
            console.log(vm.searchOptions);
            // console.log(vm.projectEntity);
            var searchOptions = vm.searchOptions;
            $("#jqGrid").jqGrid("setGridParam", {postData: searchOptions});
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
});
